<?php
include('../conn.php');
/*
从购物车获取数据添加订单
*/
error_reporting(0);
// 需传参数
$userid 			= 	$_REQUEST['userid'];
$addressid 			= 	$_REQUEST['addressid'];
$buyFreightRisk 	= 	$_REQUEST['buyFreightRisk'];
$distribution_fee 	= 	$_REQUEST['distribution_fee'];
$goodsid 			= 	$_REQUEST['goodsid'];
$addnum 			= 	$_REQUEST['addnum'];
$fTitle 			= 	$_REQUEST['fTitle'];
$sTitle 			= 	$_REQUEST['sTitle'];
$msg 				= 	$_REQUEST['msg'];
$fanquanid 			= 	$_REQUEST['fanquanid'];
$usersFirstId		=	$_REQUEST['usersFirstId'];
// 需传参数 end

// 解析 
$fanquanids = explode(',', $fanquanid);
// 解析 end

// 生成订单号函数
function build_order_no(){
    return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}


// 生成订单号函数 end

// 状态 
$state = 0;
$state1 = 1;
$state2 = 2;
// 状态 end

if ($userid != '' && $buyFreightRisk != '' && $goodsid != '' && $addnum != '' && $distribution_fee != '') {
	// 查询商品信息
	$query = mysql_query("select * from goods where goodsid = ".$goodsid);
	$goods = mysql_fetch_assoc($query);
	$price = $goods[price];
	$shopid = $goods[shopid];
	$title = $goods[title];
	// 查询商品信息 end

	// 查找送货地址
	if ($addressid != '') {
		$query = mysql_query("select * from address where id=".$addressid);
	}else{
		// 默认地址
		$query = mysql_query("select * from address where userid=".$userid." and state=".$state1);
		// 默认地址 end
	}
	$myaddress = mysql_fetch_assoc($query);
	$address=$myaddress[province].",".$myaddress[city].",".$myaddress[quyu].",".$myaddress[address].",".$myaddress[name].",".$myaddress[tel];

	// 查找送货地址 end

	// 查询商家信息 
	$query = mysql_query("select *,(SQRT(POWER(MOD(ABS(longitude - {$myaddress['longitude']}),360),2) + POWER(ABS(latitude - {$myaddress['latitude']}),2))*160) AS distance from shop where shopid =".$shopid);

	$shop = mysql_fetch_assoc($query);
	// 查询商家信息 end

	// 生成订单号
	$numbers = build_order_no();
	// 生成订单号 end

	// 区分海淘和快购
	$sql = mysql_query("select type from zifenlei where id=(select zixilie from goods where goodsid=".$goodsid.")");
	$zixilie = mysql_fetch_assoc($sql);
	$type = $zixilie[type];
	// 区分海淘和快购 end

	$totalprice = $price*$addnum;

	if ($type == 0) { //海淘

		// 计算运费险
		if ($buyFreightRisk == 1) {
			$query = mysql_query("select * from freightrisk");
			$freightRisk = 0;
			while ($row = mysql_fetch_assoc($query)) {
				if ($addnum >= $row[goodsNum]) {
					$freightRisk = $row[freightRisk];
				}
			}
			$totalprice+= $freightRisk;
		}
		// 计算运费险 end

		// 使用返券 
		if ($fanquanids[0] != 0) {
			// 计算价格 
			$query = mysql_query("select price from myfanquan where fanquanid=".$fanquanids[0]."");
			$fanquan_price = mysql_fetch_assoc($query);
			$totalprice -= $fanquan_price['price'];
			// 计算价格 end

			// 更新返券为已经使用
			$query = mysql_query("UPDATE myfanquan set is_use = ".$state1." where fanquanid=".$fanquanids[0]);
			// 更新返券为已经使用 end
		}
		// 使用返券 end


		//加上快递费
		$totalprice += $distribution_fee;
		// var_dump($totalprice);die;
		// 生成订单
		$xia_dan_datetime = date("Y-m-d H:i:s");//生成下单时间
		$query = mysql_query("INSERT INTO goodsorders(numbers,totalprice,userid,address,state,shopid,buyFreightRisk,msg,type,distribution_fee,xia_dan_datetime) VALUES ('$numbers','$totalprice','$userid','$address','$state','$shopid','$buyFreightRisk','$msg','$type','$distribution_fee','$xia_dan_datetime')");
		// 生成订单 end

		$ordersid = mysql_insert_id();
		// 计算平台返劵
		$cateid=mysql_fetch_assoc( mysql_query('select xilie from goods where goodsid='.$goodsid) )['xilie'];
	    $quanrate=mysql_fetch_assoc( mysql_query('select rate from categroy where xilie='.$cateid) )['rate'];

	    //计算店铺返佣
	    $cateprice=mysql_fetch_assoc( mysql_query('select rates from shop where shopid='.$shopid) )['rates'];
	    $blance_price = $totalprice-$totalprice * ($quanrate+$cateprice)/100;
		mysql_query("UPDATE goodsorders SET blance_price=".$blance_price." where numbers=".$numbers);

	}elseif ($type == 1) { //快购
		$totalprice += $shop[distribution_fee];

		// 使用返券 
		if ($fanquanids[1] != 0) {
			// 计算价格 
			$query = mysql_query("select price from myfanquan where fanquanid=".$fanquanids[1]."");
			$fanquan_price = mysql_fetch_assoc($query);
			$totalprice -= $fanquan_price['price'];
			// 计算价格 end

			// 更新返券为已经使用
			$query = mysql_query("UPDATE myfanquan set is_use = ".$state1." where fanquanid=".$fanquanids[1]);
			// 更新返券为已经使用 end
		}
		// 使用返券 end

		// 生成订单
		$query = mysql_query("INSERT INTO goodsorders(numbers,totalprice,userid,address,state,shopid,msg,type,distribution_fee) VALUES ('$numbers','$totalprice','$userid','$address','$state','$shopid','$msg','".$shop[distribution_fee]."')");
		// 生成订单 end
		$ordersid = mysql_insert_id();
		// 计算平台返劵
		$cateid=mysql_fetch_assoc( mysql_query('select xilie from goods where goodsid='.$goodsid) )['xilie'];
	    $quanrate=mysql_fetch_assoc( mysql_query('select rate from categroy where xilie='.$cateid) )['rate'];

	    //计算店铺返佣
	    $cateprice=mysql_fetch_assoc( mysql_query('select rates from shop where shopid='.$shopid) )['rates'];
	    $blance_price = $totalprice-$totalprice * ($quanrate+$cateprice)/100;
		mysql_query("UPDATE goodsorders SET blance_price=".$blance_price." where numbers=".$numbers);
	}

	// 记录到购物车
	$srats=mysql_query("INSERT INTO car(userid,goodsid,goodsname,fTitle,sTitle,price,state,ordersid,number,shopid) VALUES ('$userid','$goodsid','$title','$fTitle','$sTitle','$totalprice','$state2','$ordersid','$addnum','$shopid')");
	// 记录到购物车 end

	// 更新商品的销量和库存
	$query = mysql_query("update goods set number=number-".$addnum.",xiaoliang=xiaoliang+".$addnum." where goodsid=".$goodsid);
	// 更新商品的销量和库存 end

	// 减少该商品规格的数量
	if ($sTitle != '') {
		$query = mysql_query("update goodsselect set number=number-".$addnum." where goodsid=".$goodsid." and fTitle='".$fTitle."' and sTitle='".$sTitle."'");
	}else{
		$query = mysql_query("update goodsselect set number=number-".$addnum." where goodsid=".$goodsid." and fTitle='".$fTitle."'");
	}
	// 减少该商品规格的数量 end

	// 上架与否
	$query = mysql_query("select number from goods where goodsid=".$goodsid);
	$newgoods = mysql_fetch_assoc($query);
	$number = $newgoods[number];
	if ($number <=0) {
		$query = mysql_query("UPDATE goods set shangjia=".$state." where goodsid=".$goodsid);
	}
	// 上架与否 end

	// 调用信息推送接口
	$url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state."&ordersid=".$ordersid;
	file_get_contents($url);
	// 调用信息推送接口 end

	$list = array('numbers' => $numbers, 'ordersid' => $ordersid, 'total' => $totalprice);

	$arr = array('code' => 200, 'msg' => '添加订单成功', 'list' => $list);
}else{
	$arr = array('code' => 400, 'msg' => 'userid或goodsid或addnum或fTitle或buyFreightRisk不存在,可传参数addressid,sTitle');
}

echo json_encode($arr);

?>